package main

import (
	"context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"inventory_srv/inventory"
	"log"
	"sync"
)

func main() {
	conn, err := grpc.Dial("10.3.189.12:50001", grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Println(err)
		return
	}
	client := inventory.NewInventoryClient(conn)

	var gInfo []*inventory.GoodsInvInfo

	gInfo = append(gInfo, &inventory.GoodsInvInfo{
		GoodsId: 1,
		Stock:   1,
	})

	wg := sync.WaitGroup{}
	wg.Add(100)

	for i := 0; i < 100; i++ {
		go func() {
			_, err = client.Sell(context.Background(), &inventory.SellInfo{
				GoodsInfo: gInfo,
			})
			if err != nil {
				log.Println(err)
				return
			}
			log.Println("扣减成功")
			wg.Done()
		}()
	}
	wg.Wait()
}
